(function() {
  app.controller('forgetpwCtrl', function($rootScope, $scope, $state, tradeApi, alertTip, $interval, $timeout) {
    $scope.client = {};

    var num = 60;
    var count = 0; // 防止多次点击
    $scope.get_code_color = 1;
    $scope.code_tip = '获取验证码';

    $scope.get_code = function() {
      if (!$scope.client.mobile) return;
      count++;
      if (count !== 1) { return; }

      tradeApi.query({
        funcId: '300021',
        data: {
          auth_type: '1',
          mobile: $scope.client.mobile
        }
      })
      .then(function(res) {
        $scope.get_code_color = 0;
        var timer = $interval(function() {
          num--;
          $scope.code_tip = num + '秒后重新获取';
        }, 1000, 59).then(function() {
          $scope.get_code_color = 1;
          $scope.code_tip = '重新获取验证码';
          num = 60;
          count = 0;
        });

      })
      .catch(function(){
        count = 0;
      });

    };

    $scope.change = function() {
      if ($scope.client.new_password === $scope.client.renew_password) {
        tradeApi.query({
          funcId: '300008',
          data: $scope.client
        }).then(function(res) {
          alertTip.open({
            state: 1,
            msg: '重置密码成功'
          });
          $timeout(function() {
            $state.go('login');
          }, 1000);
        });
      } else {
        alertTip.open({
          state: 0,
          msg: '两次密码不一致'
        });
      }
    };
  });
})();